﻿using System;
using System.Collections.Generic;

class SumOfTwoPositiveIntegers
{
    //Exercise 8.
    //Write a method that adds two positive integer numbers represented as arrays of digits (each array element arr[i] 
    //contains a digit; the last digit is kept in arr[0]). Each of the numbers that will be added could have up to 10 000 digits.

    //783, 915   /   615, 593

    static void Main()
    {
        Console.Write("Enter the first positive integer: ");
        string firstNumStr = Console.ReadLine();
        int firstNum = int.Parse(firstNumStr);
        Console.Write("Enter the second positive integer: ");
        string secondNumStr = Console.ReadLine();
        int secondNum = int.Parse(secondNumStr);

        int[] firstNumber = new int[firstNumStr.Length];
        for (int i = 0; i < firstNumStr.Length; i++)
        {
            firstNumber[i] = firstNum % 10;
            firstNum = firstNum / 10;
        }

        int[] secondNumber = new int[secondNumStr.Length];
        for (int i = 0; i < secondNumStr.Length; i++)
        {
            secondNumber[i] = secondNum % 10;
            secondNum = secondNum / 10;
        }

        AddNumbers(firstNumStr, secondNumStr, firstNumber, secondNumber);
    }

    private static void AddNumbers(string firstNumStr, string secondNumStr, int[] firstNumber, int[] secondNumber)
    {
        //define the length, i.e. the number of digits of the result number
        int newNumberLength = 0;
        if (firstNumStr.Length > secondNumStr.Length)
        {
            newNumberLength = firstNumStr.Length + 1;
        }
        else
        {
            newNumberLength = secondNumStr.Length + 1;
        }

        int[] newNumber = new int[newNumberLength];

        for (int i = 0; i < newNumber.Length; i++)
        {
            if (i < firstNumStr.Length)
            {
                newNumber[i] = newNumber[i] + firstNumber[i];
                if (newNumber[i] >= 10)
                {
                    newNumber[i] = newNumber[i] - 10;
                    newNumber[i + 1] = newNumber[i + 1] + 1;
                }
            }
            if (i < secondNumStr.Length)
            {
                newNumber[i] = newNumber[i] + secondNumber[i];
                if (newNumber[i] >= 10)
                {
                    newNumber[i] = newNumber[i] - 10;
                    newNumber[i + 1] = newNumber[i + 1] + 1;
                }
            }
        }

        //print the new number        
        for (int i = newNumber.Length - 1; i >= 0; i--)
        {
            Console.Write(newNumber[i]);
        }
        Console.WriteLine();

    }

}

